package com.bw.repair

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Adapter
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.mvvm.BaseActivity
import com.bw.repair.databinding.ActivityAddRepairBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/repair/AddRepairActivity")
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding,RepairViewModel>() {
    var area = "A区停车场"
    val imgList = arrayListOf<String>()
    val imgAdapter by lazy { ImgAdapter() }
    override fun initView() {
        val location = arrayOf("请选择位置", "A区停车场", "B区停车场", "C区停车场")
        val locationAdapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,location)
        locationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairlocation.adapter = locationAdapter
        binding.repairlocation.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area = location[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }

        binding.headOk.setOnClickListener {
            val contact = binding.repaircontact.text.toString()
            val desc = binding.repairdesc.text.toString()
            val order = binding.repairorder.text.toString()
            val name = binding.repairName.text.toString()
            val sdf = SimpleDateFormat("YYYY-MM-dd HH:mm:ss")
            val item = RepairBeanItem(contact,desc,"",0,order,"待处理",area,name,sdf.format(System.currentTimeMillis()),1)
            viewModel.addRepair(item)
        }

        binding.repairImg.setOnClickListener {
            if (imgAdapter.data.size == 4){
                ToastUtils.showLong("最多添加4张图片")
                return@setOnClickListener
            }
            PictureSelector.create(this@AddRepairActivity,200).selectPicture()
        }

        binding.rvImg.apply {
            adapter = imgAdapter
            layoutManager = GridLayoutManager(this@AddRepairActivity,3)
        }




    }

    override fun initData() {
        viewModel.addData.observe(this){
            viewModel.addRepairRoom(it)
            finish()
        }
        viewModel.addRepairRoomData.observe(this){
            ToastUtils.showLong("添加成功")
            Log.i("zzzzzzzzzzzzzzzz", it.toString())
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
//            imgList.add(bean.path)
            imgAdapter.data.add(bean.path)
            imgAdapter.notifyDataSetChanged()

        }
    }
}